package com.zhou.base.components.feign.simple.controller;

import cn.hutool.core.map.MapUtil;
import com.zhou.base.components.bs.common.model.Response;
import com.zhou.base.components.feign.simple.feign.TestUrlConnectionRefusedFeign;
import com.zhou.base.components.feign.simple.feign.TestUrlFeign;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 测试类
 *
 * @author xianfeng
 * @date 2023/11/22 16:21
 * @description:
 */
@RequiredArgsConstructor
@RefreshScope
@RestController
@RequestMapping("/testFeign")
public class TestFeignController {

    private final TestUrlFeign testUrlFeign;

    private final TestUrlConnectionRefusedFeign connectionRefusedFeign;


    @GetMapping("/normal")
    public Response queryAuthByFegin(String param) {
        Response<String> feignResp = testUrlFeign.queryByAuth(param);
        return Response.success(feignResp);
    }

    @GetMapping("/error")
    public Response queryWithStatus(String param) {
        Response<String> feignResp = testUrlFeign.queryWithError(param);
        return Response.success(feignResp);
    }

    @GetMapping("/errorStatus")
    public Response queryWithErrorStatus(String param) {
        Response<String> feignResp = testUrlFeign.queryWithErrorStatus(param);
        return Response.success(feignResp);
    }

    @GetMapping("/404")
    public Response testQuery404() {
        Response<String> stringResponse = testUrlFeign.query404();
        return Response.success();
    }

    @GetMapping("/connectionRefused")
    public Response connectionRefused() {
        Response<String> stringResponse = connectionRefusedFeign.getToken("111", "222");
        return Response.success();
    }

    @ApiOperation("测试错误的状态码")
    @PostMapping(value = "/upload", headers = "Content-Type=multipart/form-data")
    public Response upload(@RequestParam("file") MultipartFile file) {
        Map<String, Object> headers = MapUtil.newHashMap();
        headers.put("Authorization11", "test");
        headers.put("DeptCode", 1000);

        Response<String> upload = testUrlFeign.upload(file, headers);
        return Response.success(upload.getData());
    }
}
